#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char* argv[]) {
	int sockfd, connfd;
	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	int len = sizeof(server_addr);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	server_addr.sin_port = htons(4567);

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	bind(sockfd, (struct sockaddr*)&server_addr, len);
	listen(sockfd,20);

	while (1) {
		connfd = accept(sockfd, (struct sockaddr*)&client_addr, &len);

		struct timeval tv;
		tv.tv_sec  = 5;
		tv.tv_usec = 0;
		setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));

		char buf[100] = {0};
		if (read(connfd, buf, 100) < 0)
			printf("timeout\n");
					
		printf("svr_read: %s\n", buf);
		write(connfd, buf, strlen(buf));
		close(connfd);
	}
	close(sockfd);
	return 0;
}